package cn.hsa.xxx.approve.controller;


import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import cn.hsa.xxx.approve.service.ConsumerService;
import cn.hsa.xxx.api.dto.ShelvesApprovalProcessDTO;
import cn.hsa.xxx.approve.service.ProducerService;
import io.swagger.annotations.Api;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Queue;

@RequestMapping("/web/approve/Approve")
@RestController
@Slf4j
@Api(tags = "审批上架")
public class ApprovalController {

    @Autowired
    private ProducerService producerService;

    @Autowired
    private  ConsumerService consumerService;


    @PutMapping("/producer")
    public WrapperResponse<String> producer(@RequestBody ShelvesApprovalProcessDTO shelvesApprovalProcessDTO) {
        boolean flag = producerService.sendMsgDTO(shelvesApprovalProcessDTO);
        String msg = flag == true ? "成功发起书籍上架请求" : "发起上架请求失败，出现异常";
        return WrapperResponse.success(msg);
    }

    @GetMapping("/getQueue")
    public WrapperResponse<Queue<ShelvesApprovalProcessDTO>> getMqQueue() {
        Queue<ShelvesApprovalProcessDTO> queue = consumerService.getQueue();
        return WrapperResponse.success(queue);
    }

    @PutMapping("/approveDtoList")
    public WrapperResponse<String> approveDtoList(Boolean flag) throws Exception {
        Pair<String, Boolean> pair = consumerService.addApproveDtoList(flag);
        if(pair.getKey() == "-1") {
            return WrapperResponse.success("当前没有上架申请");
        }
        String result = "id为"+pair.getKey()+"上架请求审批"+(pair.getValue() == true ? "通过" : "不通过");
        return WrapperResponse.success(result);
    }
}
